package GUI;

import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
import javax.swing.AbstractListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JScrollPane;
import javax.swing.JScrollBar;
import javax.swing.JLabel;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

import java.awt.Font;
import java.awt.Color;

import javax.swing.JTextField;
import javax.swing.JButton;




import UserData.User_Info;
import UserData.User_Management;

import com.toedter.calendar.JCalendar;
import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JDateChooserBeanInfo;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTable;
import javax.swing.JRadioButton;

public class NormalUser extends JFrame {

	private JPanel contentPane;
	private JTabbedPane tabbedPane;
	private JPanel panelMain;
	private JButton btnMain;
	private JLabel lblCurrentTime;
	private JLabel lblNextMeeting;
	private JPanel panelGraph;
	private BarChart bc;
	private DigitalClock digitclock;
	private MyPage mypagetabbedPane;
	private Compare comparetabbledPane;
	private BarChart barcharttabbedPane;
	private JScrollBar scrollBar;
	private JScrollPane scrollPane;
	private JCheckBox checkBoxFriend[];
	private int UserListNumber = User_Management.Get_User_list.size();
	private JPanel testPane;

	int mainBtnCount;
	
	int time[];
	String name[];

	private void initVar(){
		Runnable thj= new AlarmRunner();
		Thread th = new Thread(thj);
		th.start();
		contentPane = new JPanel(){
			@Override
			public void paintComponent(Graphics g){
				Image image = new ImageIcon("normaluser.png").getImage();
				g.drawImage(image, 0, 0, this);
			}      
		};
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
      mypagetabbedPane = new MyPage();
     // barcharttabbedPane = new BarChart();
      panelMain = new JPanel();
      comparetabbledPane = new Compare();
      btnMain = new JButton("Button"){
    	  public void paintComponent(Graphics g){
    		  Image image = new ImageIcon("car.png").getImage();

    		  g.drawImage(image, 0, 0, this);
    	  }      
      };
      btnMain.setBorder(null);
      btnMain.addActionListener(new ActionListener() {
    	  public void actionPerformed(ActionEvent arg0) {

    	  }
      });
      btnMain.addActionListener(new ActionListener() {
    	  public void actionPerformed(ActionEvent e) {
    		  btnMainActionPergormed(e);
    	  }
      });



      digitclock=new DigitalClock("HHmmss");
      lblCurrentTime = new DigitalClock();
      lblCurrentTime.setForeground(Color.GRAY);
      lblCurrentTime.setFont(new Font("Century Gothic", Font.BOLD, 90));
      lblNextMeeting = new JLabel("Next Meeting");

      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
      setContentPane(contentPane);
      contentPane.setLayout(null);


      tabbedPane.setBounds(56, 38, 730, 500);
      contentPane.add(tabbedPane);




      Icon tabmain = new ImageIcon("tabmain.png");
      Icon tabmypage = new ImageIcon("tabmypage.png");
      Icon tabgraph = new ImageIcon("tabgraph.png");
      Icon tabcompare = new ImageIcon("tabcompare.png");

      tabbedPane.addTab("", tabmain, panelMain, null);
      panelMain.setLayout(null);



      btnMain.setBounds(247, 327, 97, 23); 

      btnMain.setBounds(250, 320, 250, 100);//if(占쏙옙占썲가占쏙옙占쏙옙擔챨占�-15<占쏙옙占쏙옙챨占�&&占쏙옙튼占쏙옙 占싼뱄옙占쏙옙 占쏙옙占쏙옙占쏙옙 占십억옙占쏙옙占쏙옙){  btnMain enable } 

      panelMain.add(btnMain);



      lblCurrentTime.setBounds(206, 64, 410, 173);
      panelMain.add(lblCurrentTime);

      lblNextMeeting.setBounds(250, 235, 97, 39);
      panelMain.add(lblNextMeeting);


      tabbedPane.addTab("", tabmypage, mypagetabbedPane, null);

      tabbedPane.addTab("", tabcompare, comparetabbledPane, null);


      panelGraph = new JPanel();
      panelGraph.setLayout(null);

      tabbedPane.addTab("", tabgraph, panelGraph, null);

     
      checkBoxFriend = new JCheckBox[UserListNumber];
      
      panelGraph.setLayout(null);
           
      scrollPane = new JScrollPane();
      scrollPane.setBounds(36, 48, 157, 318);
      panelGraph.add(scrollPane);
      
      scrollBar = new JScrollBar();
      scrollPane.setRowHeaderView(scrollBar);
      
      JPanel panel = new JPanel();
      scrollPane.setViewportView(panel);
      panel.setLayout(null);
      
      time=new int[UserListNumber];
      name=new String[UserListNumber];
      
      JButton btnMakeGraph = new JButton("Make Graph!");
      btnMakeGraph.addActionListener(new ActionListener() {
      	public void actionPerformed(ActionEvent e) {
      	   MGactionPerformed(e);
      	}});
      btnMakeGraph.setBounds(304, 184, 163, 23);
      panelGraph.add(btnMakeGraph);
      
    // Creating all Friends' List
    for (int i = 0; i < UserListNumber; i++) {
       checkBoxFriend[i] = new JCheckBox(User_Management.Get_User_list
             .get(i).getUser_Real_Name());
       checkBoxFriend[i].setBounds(0, 25 * i, 118, 23);
       panel.add(checkBoxFriend[i]);
       // Check If there is Log in User's Name. If there is, Do not insert that user's name at friend's list.
       if (checkBoxFriend[i].getText() == User_Management.Now_User.getUser_Real_Name()) {
          checkBoxFriend[i].setEnabled(false);
       }
    }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
      mainBtnCount=0;
	}
	public void MGactionPerformed(ActionEvent e)
	{
  		for (int i = 0; i < UserListNumber; i++){
  			if(checkBoxFriend[i].isSelected()){
  				for(User_Info tmp:User_Management.Get_User_list){
  					if(tmp.getUser_Real_Name().equals(checkBoxFriend[i].getText())){
  						time[i]=tmp.getLateTime();
  						name[i]=tmp.getUser_Real_Name();
  					}
  				}
  			}
  		BarChart graph=new BarChart(time,name);
  		graph.setVisible(true);
  		}
  	}//make graph use time array and name array as parameter of BarChart class. 
	public NormalUser() {
		initVar();
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 840, 640);
	}
	public void btnMainActionPergormed(ActionEvent e){
		int compareTime=Integer.parseInt(digitclock.getText());
		int targetTime = 0;//가장가까운시간
		if(timeCompare(targetTime, compareTime)<1500&&mainBtnCount==0){
			if(targetTime<compareTime){
				User_Management.Now_User.setLateTime(timeCompare(compareTime, targetTime));
			}
		}
	}//compare currentTime to schedule's start time.
	public int timeCompare(int big, int sm){

		int c =0;
		int temp =((big/10000) - (sm/10000));
		big=big-temp*4000;
		c=big-sm;
		return c;
	}//make method to calculate term between two time because minute is up to only 60
}



